package org.jeecg.modules.basedata.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.entity.basedata.HouseHolde;
import org.jeecg.modules.basedata.service.impl.BaseHouseHoldeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.List;

/**
* @Description: 住户台账
* @Author: jeecg-boot
* @Date:   2019-07-30
* @Version: V1.0
*/
@Slf4j
@Api(tags="住户台账")
@RestController
@RequestMapping("/b/basedata/baseHouseHolde")
public class BaseHouseHoldeController {
   @Autowired
   private BaseHouseHoldeServiceImpl baseHouseHoldeservice;


    /**
     * 住户台账查询
     *
     * @return
     */
    @AutoLog(value = "住户台账查询")
    @ApiOperation(value="住户台账查询", notes="住户台账查询")
    @PostMapping(value = "/generaTing")
    public Result<IPage<HouseHolde>> generaTing(@RequestBody HouseHolde houseHolde, HttpServletRequest req) {
        Result<IPage<HouseHolde>> result = new Result<IPage<HouseHolde>>();
        try {
            Page<HouseHolde> pageList = new Page<HouseHolde>(houseHolde.getPageNo(), houseHolde.getPageSize());
            if(houseHolde !=null && houseHolde.getFloorId()!= null && houseHolde.getFloorId().size()>0){
                pageList = baseHouseHoldeservice.getHouseholdList(pageList,houseHolde);
            }
            result.setResult(pageList);
            result.setSuccess(true);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return result;
    }

    /**
     * 收费情况导出
     * @param
     * @param
     * @return
     */
    @PostMapping(value = "/getBillsExportXls")
    public void getBillsExportXls(@RequestBody HouseHolde houseHolde, HttpServletResponse response) {
        SimpleDateFormat kpsmf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        XSSFWorkbook book = new XSSFWorkbook();
        try {
            XSSFSheet sheet = book.createSheet("mySheent");
            List<HouseHolde> list= baseHouseHoldeservice.getHouseholdListNotPage(houseHolde);
            String [] vals ={"序号","地址","住户","进户日期","开始收费日期","建筑面积","房屋性质","收费项目名称","收费标准","截止收费日期"};
            XSSFCellStyle style = book.createCellStyle();
            //居中
            style.setAlignment(HorizontalAlignment.CENTER);
            style.setVerticalAlignment(VerticalAlignment.CENTER);
            //border
            style.setBorderLeft(BorderStyle.THIN);
            style.setBorderTop(BorderStyle.THIN);
            style.setBorderRight(BorderStyle.THIN);
            style.setBorderBottom(BorderStyle.THIN);
            XSSFRow row = sheet.createRow(0);
            sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, vals.length-1));
            row.setHeight((short) 0x349);
            XSSFCell cell = row.createCell(0);
            cell.setCellValue("住户台账");
            cell.setCellStyle(style);
            row = sheet.createRow(1);
            for (int i = 0; i < vals.length; i++) {
                cell = row.createCell(i);
                cell.setCellValue(vals[i]);
                cell.setCellStyle(style);
            }
            if(list != null && list.size()>0){
                for(int i = 0;i<list.size();i++){
                    HouseHolde houseHolde1 = list.get(i);
                    String checkInDate = "";
                    String chargeDate = "";
                    String currentChargeTime = "";
                    Integer XH = 0;
                    if(houseHolde1 !=null && houseHolde1.getCheckInDate() != null ){
                        checkInDate = kpsmf.format(houseHolde1.getCheckInDate());
                    }
                    if(houseHolde1 !=null && houseHolde1.getChargeDate() != null ){
                        chargeDate = kpsmf.format(houseHolde1.getChargeDate());
                    }
                    if(houseHolde1 !=null && houseHolde1.getCurrentChargeTime() != null ){
                        currentChargeTime = kpsmf.format(houseHolde1.getCurrentChargeTime());
                    }
                    XH = i+1;
                    String[] vals2 = {XH.toString(),houseHolde1.getAddress(),houseHolde1.getCustomerName(),
                            checkInDate,chargeDate,houseHolde1.getBuildArea(), houseHolde1.getRoomProperty(),
                            houseHolde1.getFeeitemName(),houseHolde1.getFeeCurrent(),currentChargeTime};
                    createExcel(sheet, i+2, vals2,style);
                }
                book.write(generateResponseExcel("账单详情",response));
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    /**
       * @param excelName
       *   要生成的文件名字
       * @return
       * @throws IOException
       */
    private ServletOutputStream generateResponseExcel(String excelName, HttpServletResponse response) throws IOException {
        excelName = excelName == null || "".equals(excelName) ? "excel" : URLEncoder.encode(excelName, "UTF-8");
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment; filename=" + excelName + ".xlsx");
        return response.getOutputStream();
    }
    private void createExcel(XSSFSheet sheet, int rowNo, String[] vals, XSSFCellStyle style)  {
        if (vals.length > 0) {
            XSSFRow row = sheet.createRow(rowNo);
            for (int i = 0; i < vals.length; i++) {
                String val = vals[i];
                XSSFCell cell = row.createCell(i);
                cell.setCellValue(val);
                cell.setCellStyle(style);
            }
        }
    }


}
